/**
 * 初始化奖品详情对话框
 */
var WzPrizeInfoDlg = {
    wzPrizeInfoData : {},
    details:"",//奖品详情
    validateFields: {
        fileId: {
            validators: {
                notEmpty: {
                    message: '封面图id不能为空'
                }

            }
        },
        couponId: {
            validators: {
                notEmpty: {
                    message: '奖券id不能为空'
                }

            }
        },
        exchangeAmount: {
            validators: {
                notEmpty: {
                    message: '抵扣积分不能为空'
                },
                regexp: {
                    regexp: /^[0-9]+$/,
                    message: '请输入正整数！'
                }

            }
        },
        title: {
            validators: {
                notEmpty: {
                    message: '主标题不能为空'
                }

            }
        },
        subtitle: {
            validators: {
                notEmpty: {
                    message: '副标题不能为空'
                }

            }
        },
        payAmount: {
            validators: {
                notEmpty: {
                    message: '需要消耗金额不能为空'
                },
            regexp: {
                regexp: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/,
                message: '请输入有效数字！'
            }

            }
        },
        quantity: {
            validators: {
                notEmpty: {
                    message: '总数量不能为空'
                },
            regexp: {
                    regexp: /^[0-9]+$/,
                    message: '请输入正整数！'
                }

            }
        },
        stock: {
            validators: {
                notEmpty: {
                    message: '剩余库存不能为空'
                },
            regexp: {
                    regexp: /^[0-9]+$/,
                    message: '请输入正整数！'
                }

            }
        },
        ratio: {
            validators: {
                notEmpty: {
                    message: '中奖概率不能为空'
                },
            regexp: {
                    regexp: /^[0-1]$|^0\.[0-9]+$/,
                    message: '请输入0-1之间的数字！'
                }

            }
        }
    }

};

/**
 * 清除数据
 */
WzPrizeInfoDlg.clearData = function() {
    this.wzPrizeInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
WzPrizeInfoDlg.set = function(key, val) {
    this.wzPrizeInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
WzPrizeInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
WzPrizeInfoDlg.close = function() {
    parent.layer.close(window.parent.WzPrize.layerIndex);
}

/**
 * 收集数据</div>
 */
WzPrizeInfoDlg.collectData = function() {
    this
    .set('couponId')
    .set('prizeId')
    .set('title')
    .set('subtitle')
    .set('coverUrl')
    .set('payAmount')
    .set('quantity')
    .set('stock')
    .set('label')
    .set('ratio')
    .set('sort')
    .set('type')
    .set('subtype')
    .set('status')
    .set('createTime')
    .set('updateTime')
    .set('modifierId')
    .set('fileId')
    .set('couponId')
    .set('contentType')
    .set('content')
    .set('exchangeAmount');
}
/**
 * 验证数据是否为空
 */
WzPrizeInfoDlg.validate = function () {
    $('#WzPrizeInfoForm').data("bootstrapValidator").resetForm();
    $('#WzPrizeInfoForm').bootstrapValidator('validate');
    return $("#WzPrizeInfoForm").data('bootstrapValidator').isValid();
}
/**
 * 提交添加
 */
WzPrizeInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    $("#eg").removeClass("no-padding");
    var aHTML = $('.click2edit').code();//save HTML If you need(aHTML: array).
//    if(aHTML == ""){
//        Feng.error("详情不能为空!");
//        return;
//    }
    this.details=aHTML;
    $('.click2edit').destroy();
    // 提交信息
    var a  = this.wzPrizeInfoData;
    var ajax = new $ax(Feng.ctxPath + "/wzPrize/add", function(data){
        if(data.code == 100){
            Feng.error(data.message);
        }else if(data.code == 200){
            Feng.success("添加成功!");
            window.parent.WzPrize.table.refresh();
            WzPrizeInfoDlg.close();
        }
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.wzPrizeInfoData);
    ajax.set('detail',this.details);
    ajax.start();
}

/**
 * 提交修改
 */
WzPrizeInfoDlg.editSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    $("#eg").removeClass("no-padding");
    var aHTML = $('.click2edit').code();//save HTML If you need(aHTML: array).
    this.details=aHTML;
    $('.click2edit').destroy();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/wzPrize/update", function(data){
        if(data.code == 100){
            Feng.error(data.message);
        }else if(data.code == 200){
            Feng.success("修改成功!");
            window.parent.WzPrize.table.refresh();
            WzPrizeInfoDlg.close();
        }
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.wzPrizeInfoData);
    ajax.set('detail',this.details);
    ajax.start();
}

$(function() {
    Feng.initValidator("WzPrizeInfoForm", WzPrizeInfoDlg.validateFields);
});

var edit = function () {
    $("#eg").addClass("no-padding");
    $('.click2edit').summernote({
        lang: 'zh-CN',
        focus: true
    });
};
// var save = function () {
//     $("#eg").removeClass("no-padding");
//     var aHTML = $('.click2edit').code();//save HTML If you need(aHTML: array).
//     console.log(aHTML);
//     WzPrizeInfoDlg.details=aHTML;
//     $('.click2edit').destroy();
// };

